package com.lz.football.module.playerCenter
{
	import com.global.GlobalDef;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.PlayerCenterEvent;
	import com.liangzhong.model.playerCenter.PlayerCenterDataManager;
	
	import common.BaseView.BaseView;
	import common.base.GoodsUsePanel;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	
	/**
	 *球员中心 
	 * @author Administrator
	 * 
	 */	
	public class PlayerCenterView extends BaseView implements IPlayerCenter
	{
		/**
		 *球员列表 
		 */
		private var _listView:PlayerCenterListView;
		/**
		 *阵容设置 
		 */
		private var _battleView:BattlearrayView;
		/**
		 *面板容器 
		 */
		private var viewContain:MovieClip;
		/**
		 * 
		 */
		private var _playerCenterDate:PlayerCenterDataManager;
		public function PlayerCenterView()
		{
			super();
		}
		override public function initUI(view:Object=null):void
		{
//			showListView();
			addEvtListener();
		}

		override public function removeEvtListener():void
		{
			super.removeEvtListener();	
			if(_battleView!= null)_battleView.removeEventListener(PlayerCenterEvent.REMOVE_BATTLE_VIEW,onRemoveBattleHandler);
			if(_listView != null)_listView.removeEventListener(PlayerCenterEvent.ADD_BATTLE_VIEW,onOpenBattleView);
			
		}
		override public function destroyView():void
		{
			removeEvtListener();
			if(_battleView != null)_battleView.destroyView();
			if(_listView !=  null)_listView.destroyView();
			_battleView = null;
			_listView = null;
			GoodsUsePanel.inistance.rmoveStage();
			super.destroyView();
		}
		
		/**
		 *打开阵容设置面板 
		 * @param event
		 * 
		 */
		protected function onOpenBattleView(event:Event):void
		{
			showBattleView();
		}
		/**
		 *删除阵容设置面板 
		 * @param event
		 * 
		 */		
		protected function onRemoveBattleView(event:Event):void
		{
			if(_listView == null || _listView.parent ==null)
			{
				_battleView.removeEventListener(Event.REMOVED_FROM_STAGE,onRemoveBattleView);
				AppEventDispatcher.getInstance().dispatchEvent(new PlayerCenterEvent(PlayerCenterEvent.REMOVE_MODEL));
			}
		}
		/**
		 *删除球员 
		 * @param $obj
		 * 
		 */
		public function removePlayer($obj:Object):void
		{
			if(_listView != null)_listView.removePlayer($obj);
			if(_battleView != null && _battleView.parent)
				_battleView.removePlayer($obj.id);
		}
		/**
		 *增加球员格子 
		 * 
		 */
		public function addPlayerCell($obj:Object):void
		{
			_listView.addPlayerCell($obj);
		}
		/**
		 *球员 体力和uBuff改变 
		 * @return 
		 * 
		 */
		public function changePowerAndBuff($obj:Object):void
		{
			if(_listView != null)_listView.changePowerAndBuff($obj);
			//			if(_battleView != null) _battleView.changePowerAndBuff($obj);
		}
		/**
		 *阵容改变 
		 * @param $obj
		 * 
		 */
		public function save_battlearray($obj:Object):void
		{
			if(_battleView != null)_battleView.save_battlearray($obj);
		}
		/**
		 *更换装备 
		 * @param $obj
		 * 
		 */
		public function replace_equip($obj:Object):void
		{
			if(_listView != null)_listView.updataEquipInfo($obj)
		}
		/**
		 *修改球员号码 
		 * @param $obj
		 * 
		 */
		public function changePlayerNumber($obj:Object):void
		{
			if(_listView != null)_listView.changePlayerNumber($obj);
		}
		/**
		 *增加球员 
		 * @param $obj
		 * 
		 */
		public function addPlayer($obj:Object):void
		{
			if(_listView != null)_listView.upDataPlayer();
			
			if(_battleView != null && _battleView.parent)
				_battleView.addPlayer($obj.playerId);
		}
		
		/**
		 *显示球员列表界面 
		 * 
		 */		
		
		private function showListView():void
		{
			if(_listView == null)
			{
				_listView = new PlayerCenterListView();
				_listView.initUI();
				_listView.addEventListener(PlayerCenterEvent.ADD_BATTLE_VIEW,onOpenBattleView);
			}
			GlobalDef.GameWindowContainer.addChild(_listView);
		}
		/**
		 *显示阵容设置界面 
		 * 
		 */
		private function showBattleView():void
		{
			if(_battleView == null)
			{
				_battleView = new BattlearrayView();
				_battleView.initUI();
				_battleView.addEventListener(PlayerCenterEvent.REMOVE_BATTLE_VIEW,onRemoveBattleHandler);
			}
			if(_battleView.parent == null)_battleView.showUseBattle();
			GlobalDef.GameWindowContainer.addChild(_battleView);
		}
		/**
		 *删除阵容设置 面板 
		 * @param event
		 * 
		 */		
		protected function onRemoveBattleHandler(event:Event):void
		{
			// TODO Auto-generated method stub
			if(_listView == null ||_listView.parent == null)
			{
				AppEventDispatcher.getInstance().dispatchEvent(new PlayerCenterEvent(PlayerCenterEvent.REMOVE_MODEL));
			}
		}
		/**
		 *显示不同的面板{type ,2}
		 * 显示面板 {type:2}
		 * 2为打开阵容设置面板 
		 * @param $obj
		 * 
		 */
		public function showViewType($obj:Object):void
		{
			if($obj == null)
			{
				showListView();
			}else
			{
				showBattleView();
			}
			
		}
		
		//			_windowBg = Reflection.createInstance( "com.lz.modul.playerCenter.Bg");
		//			this.addChild(_windowBg);
		//			symbolSkin = Reflection.createInstance("com.lz.modul.playerCenter.MainView");
		//			this.addChild(symbolSkin);
		//			viewContain = symbolSkin.viewContain;
		//			isTurnTop = true;
		
		//			txtAddNum = symbolSkin.infoMc.txtAddNum;
		//			txtFactor = symbolSkin.infoMc.txtFactor;
		//			upgradeBtn = symbolSkin.infoMc.upgradeBtn;
		//			txtLeftTime = symbolSkin.infoMc.txtLeftTime;
		//			txtTimeDesc = symbolSkin.infoMc.txtTimeDesc;
		//			LvMc = symbolSkin.infoMc.LvMc
		//			txtLeftTime.text = "";
		//			txtLeftTime.visible = false;
		//			upgradeBtn.visible = false;
		//			txtTimeDesc.visible = false;
		//			
		//			moraleText = symbolSkin.morale.moraleText;
		//			progress = symbolSkin.morale.progress;
		//			moraleBtn = symbolSkin.morale.moraleBtn;
		//			
		//			_tabBtn = new TabBtnMenuPanel();
		//			_tabBtn.init(symbolSkin,2);
		//			_tabBtn.setSelectStatus(TabBtnConst.ONE_CONST);
		//			isDrag = true;
		//			isCloseBtn = true;
		//			super.initUI();
		//			moveClose(660,15);
		//			if(_listView == null)
		//			{
		//				_listView = new PlayerCenterListView();
		//				_listView.initUI();
		//				viewContain.addChild(_listView);
		//			}
		//			
		//			_playerCenterDate = AppData.getInstance().playerCenterDataManager;
		//			initData();
		//			DisplayMouseEventManager.instance.addTipsDisplay(moraleBtn,"增加士气");
		
		
		//		private function initData():void
		//		{
		//			LvMc.gotoAndStop(_playerCenterDate.playerCenterVo.level +1);
		//			
		//			txtFactor.text = "升级需要欧元"+AppData.getInstance().playerCenterDataManager.playerCenterVo.coin  + "，俱乐部等级："+ AppData.getInstance().playerCenterDataManager.playerCenterVo.needlevel;
		//			txtAddNum.text = "增加一个球员位";
		//			//冷却时间
		//			var curSeconds 		:Number = AppData.getInstance().time;
		//			//当前时间与上次升级时间间隔 秒
		//			var grapTime :Number = curSeconds - AppData.getInstance().playerCenterDataManager.playerCenterVo.pretime
		//			//如果大于冷却时间 可以再次升级
		//			if(grapTime >= AppData.getInstance().playerCenterDataManager.playerCenterVo.freezingTime)
		//			{
		//				txtLeftTime.visible = false;
		//				txtTimeDesc.visible = false;
		//				upgradeBtn.visible = true;
		//				TimerManager.instance.removeFunction(onTiming);
		//				txtLeftTime.removeEventListener(TextEvent.LINK,onDropTimeHandler);
		//			}
		//			else//显示冷却时间
		//			{
		//				leftTime = AppData.getInstance().playerCenterDataManager.playerCenterVo.freezingTime - grapTime;
		//				txtLeftTime.visible = true;
		//				txtTimeDesc.visible = true;
		//				upgradeBtn.visible = false;
		//				txtLeftTime.htmlText = "" + DateUtil.formatShowHMS(leftTime,true);
		//				txtLeftTime.addEventListener(TextEvent.LINK,onDropTimeHandler);
		//				TimerManager.instance.addFunction(onTiming);
		//			}
		//			if(AppData.getInstance().playerCenterDataManager.playerCenterVo.level >= 9)
		//			{
		//				upgradeBtn.visible = false;
		//				TimerManager.instance.removeFunction(onTiming);
		//				txtLeftTime.visible = false;
		//				txtTimeDesc.visible = false;
		//				txtFactor.text = "";
		//				txtAddNum.text = "";
		//			}
		//			addMornal(null);
		//		}
		//		/**
		//		 *时间计时 
		//		 */		
		//		private function onTiming():void
		//		{
		//			leftTime --;
		//			if(leftTime <= 0)
		//			{
		//				txtLeftTime.visible = false;
		//				txtTimeDesc.visible = false;
		//				upgradeBtn.visible = true;
		//				TimerManager.instance.removeFunction(onTiming);
		//			}
		//			else
		//			{
		//				txtLeftTime.htmlText = "<u><a href='event:dropTime'>" + DateUtil.formatShowHMS(leftTime,true) + "</a><u>";
		//			}
		//		}
		//		/**
		//		 *去除冷却时间 
		//		 * @param e
		//		 * 
		//		 */		
		//		private function onDropTimeHandler(e :Event):void
		//		{
		//			var info :String = "您确定花费200点券去除冷却时间?";
		//			Alert.show(info,"",Alert.YES | Alert.NO,GlobalDef.AlertContainer,dropTimeAlertHandler);
		//		}
		//		/**
		//		 * 去除冷却时间 确认
		//		 * @param type
		//		 * 
		//		 */		
		//		private function dropTimeAlertHandler(type :int):void
		//		{
		//			if(type == Alert.YES)
		//			{
		//				AppEventDispatcher.getInstance().dispatchEvent(new PlayerCenterEvent(PlayerCenterEvent.CLEAR_UP_TIME));
		//			}
		//		}
		
		
		
		//		/**
		//		 *打开使用士气面板 
		//		 * @param event
		//		 * 
		//		 */
		//		protected function onMoraleHandler(event:MouseEvent = null):void
		//		{
		//			//			Alert.showErrorTip("增加士气 ");
		//			if(AppData.getInstance().clubDatamanager.clubInfo.morale >=5)
		//			{
		//				Alert.showErrorTip("士气已满，不需要增加士气");
		//				return;
		//			}
		//			GoodsUsePanel.inistance.initPlace();
		//			GoodsUsePanel.inistance.setShowGoodsType(GoodsType.OTHER,OtherGoodType.MORALE);
		//			GoodsUsePanel.inistance.callBack = usemoraleGoods;
		//			GoodsUsePanel.inistance.showBtn(2);
		//			
		//		}
		//		/**
		//		 * 使用士气 物品 
		//		 * @param $goodsVo
		//		 * 
		//		 */
		//		private function usemoraleGoods($goodsVo:GoodsVo):void
		//		{
		//			if($goodsVo == null)
		//			{
		//				Alert.show("确定花费100点券来增加士气么？","",Alert.NO,GlobalDef.AlertContainer,useItem)
		//				function useItem($type:int):void
		//				{
		//					if($type == Alert.YES)
		//						AppEventDispatcher.getInstance().dispatchEvent(new PlayerCenterEvent(PlayerCenterEvent.ADD_MORALE,{cellId:0}));
		//				}
		//				return;
		//			}
		//			var cellId:int;
		//			cellId = $goodsVo.pid;
		//			AppEventDispatcher.getInstance().dispatchEvent(new PlayerCenterEvent(PlayerCenterEvent.ADD_MORALE,{cellId:cellId}));
		//		}
		//		protected function onTabHandler(event:Event):void
		//		{
		//			while(viewContain.numChildren)
		//				viewContain.removeChildAt(0);
		//			switch(_tabBtn.selectType)
		//			{
		//				case TabBtnConst.ONE_CONST:
		//					viewContain.addChild(_listView);
		//					_listView.upDataPlayer();
		//					break;
		//				case TabBtnConst.TWO_CONST:
		//					
		//					if(_battleView != null)
		//						_battleView.setUpdataInfo();
		//					if(_battleView == null)
		//					{
		//						_battleView = new BattlearrayView();
		//						_battleView.initUI();
		//					}
		//					viewContain.addChild(_battleView);
		//					break;
		//			}
		//			GoodsUsePanel.inistance.rmoveStage();
		//		}
		
		//		/**
		//		 *升级 
		//		 * @param event
		//		 * 
		//		 */
		//		protected function onUpgradeHandler(event:MouseEvent):void
		//		{
		//			
		//			if(_playerCenterDate.playerCenterVo.coin > AppData.getInstance().clubDatamanager.clubInfo.coin)
		//			{
		//				Alert.showErrorTip("欧元不足，不可以升级");
		//				return ;
		//			}
		//			if(_playerCenterDate.playerCenterVo.needlevel > AppData.getInstance().clubDatamanager.clubInfo.level)
		//			{
		//				Alert.showErrorTip("欧元不够，不可以升级");
		//				return ;
		//			}
		//			Alert.show("升级需要消耗" +_playerCenterDate.playerCenterVo.coin+"欧元","",Alert.NO,GlobalDef.AlertContainer,upGradeAlert);
		//			function upGradeAlert(type:int):void
		//			{
		//				if(type == Alert.YES)
		//					AppEventDispatcher.getInstance().dispatchEvent(new PlayerCenterEvent(PlayerCenterEvent.UPGRADE_PLAYERCENTER));
		//			}
		//			
		//		}
		//		/**
		//		 *关闭面板 
		//		 * @param event
		//		 * 
		//		 */
		//		override public function onCloseHandler(event:MouseEvent):void
		//		{
		//			AppEventDispatcher.getInstance().dispatchEvent(new PlayerCenterEvent(PlayerCenterEvent.REMOVE_MODEL));
		//		}
		//		/**
		//		 *球员中心升级 
		//		 * @param $obj
		//		 * 
		//		 */
		//		public function playerCenterUpGrade($obj:Object):void
		//		{
		//			upgradeBtn.addEventListener(MouseEvent.CLICK,onUpgradeHandler);
		//			initData();
		//			addPlayerCell($obj);
		//		}
		//		/**
		//		 *清除冷却时间 
		//		 * @param $obj
		//		 * 
		//		 */
		//		public function clearUpTime($obj:Object):void
		//		{
		//			initData();
		//		}
//		/**
//		 *增加士气  
//		 * @param $obj
//		 * 
//		 */
//		public function addMornal($obj:Object):void
//		{
//			//			moraleText.text = AppData.getInstance().clubDatamanager.clubInfo.morale + "";
//			var moral:int = AppData.getInstance().clubDatamanager.clubInfo.morale
//			//			moraleText.text = (moral < 0?0:moral )+ "";
//			if(moral < 0)moral = 1;
//			//			progress.gotoAndStop(moral);
//			//			if($obj != null && GoodsUsePanel.inistance.parent)
//			//			{
//			//				GoodsUsePanel.inistance.goodsList = AppData.getInstance().bagDataManager.getGoodsListByTwoTypes(GoodsType.OTHER,OtherGoodType.MORALE);
//			//				GoodsUsePanel.inistance.callBack = usemoraleGoods;
//			//				GoodsUsePanel.inistance.showBtn(2);
//			//			}
//		}
		

		
	}
}